﻿using sqlsearcher.Models;
using sqlsearcher.Services;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sqlsearcher.Forms
{
    public partial class Login : Form
    {
        bool formMove = false;//窗体是否移动
        Point formPoint;//记录窗体的位置
        private JsonManager class1 = new JsonManager();


        public Login()
        {
            InitializeComponent();
        }


        private void Login_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                formPoint = new Point();
                int xOffset;
                int yOffset;
                if (e.Button == MouseButtons.Left)
                {
                    xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
                    yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height;
                    formPoint = new Point(xOffset, yOffset);
                    formMove = true;//开始移动
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        private void Login_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                if (formMove == true)
                {
                    Point mousePos = Control.MousePosition;
                    mousePos.Offset(formPoint.X, formPoint.Y);
                    Location = mousePos;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        private void Login_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)//按下的是鼠标左键
            {
                formMove = false;//停止移动
            }
        }

        private void Login_Load(object sender, EventArgs e)
        {
            this.BindHistory();
        }

        private void BindHistory()
        {
            cbHistorys.Items.Clear();
            var dbTypes = class1.GetDbTypes();
            var historys = class1.GetHistorys().Where(m => m.DBType == GetDBType()).ToList();
            foreach (var item in historys)
            {
                cbHistorys.Items.Add(GetSelectConnections(item));

            }
            if (historys.Count > 0)
            {
                cbHistorys.SelectedIndex = 0;
                this.txtServer.Text = historys.First().ServerName;
                this.txtUid.Text = historys.First().Uid;
                this.txtPwd.Text = historys.First().PassWord;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            var dbType = (int)AppEnum.DBType.SQLServer;
            if(rdoDB1.Checked)
                dbType = (int)AppEnum.DBType.SQLServer;
            else if(rdoDB2.Checked)
                dbType = (int)AppEnum.DBType.MySQL;
            var loginRes = TFac<SearchManager>.Instance.Login(this.txtServer.Text, this.txtUid.Text, this.txtPwd.Text, dbType);
            if (!loginRes.Item1)
            {
                MessageBox.Show("登录失败!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                TFac<Login>.Instance.Hide();
                TFac<MenuFm>.Instance.ReLoad();
                TFac<MenuFm>.Instance.Show();
            }
        }

        private void cbHistorys_SelectedIndexChanged(object sender, EventArgs e)
        {

            var historys = class1.GetHistorys().Where(m => m.DBType == GetDBType());
            if (cbHistorys.SelectedItem == null)
                return;
            var model = historys.FirstOrDefault(m => GetSelectConnections( m) == cbHistorys.SelectedItem.ToString());

            if (model != null)
            {
                this.txtServer.Text = model.ServerName;
                this.txtPwd.Text = model.PassWord;
                this.txtUid.Text = model.Uid;
            }
        }

        private string GetSelectConnections(HistoryConnInfo m)
        {
            return string.Format("{0}({1})", m.ServerName.Trim()=="."?"localhost":m.ServerName, m.Uid);

        }

        private void rdoDB2_CheckedChanged(object sender, EventArgs e)
        {
            this.BindHistory();
        }


        private int GetDBType()
        {
            if (rdoDB2.Checked)
                return (int)AppEnum.DBType.MySQL;
            return (int)AppEnum.DBType.SQLServer;
        }

        private void rdoDB1_CheckedChanged(object sender, EventArgs e)
        {
            this.BindHistory();
        }
    }
}
